﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PBMessage;
using GameUnit;
public class UISkillBar : BaseWidget
{
    public GameObject SkillBtn1;
    public GameObject SkillBtn2;
    public GameObject SkillBtnMax;
    Unit MainUnit;
    public override void OnLoad()
    {
        UIEventListener.Get(SkillBtn1).onClick += this.OnSkillBtnClick;
        UIEventListener.Get(SkillBtn2).onClick += this.OnSkillBtnClick;
        UIEventListener.Get(SkillBtnMax).onClick += this.OnSkillBtnClick;

        MainUnit = Game.Scene.Cur<GameScene>().LocalPlayer.MainUnit;
        int i = 1;
        foreach (KeyValuePair<string, Ability> Item in MainUnit.Abilities)
        {
            SkillBtn1.transform.parent.Find(i.ToString()).Find("Label").GetComponent<UILabel>().text = (string)Item.Value.TableInfo[":text"];
            i++;
        }
    }

    public void OnSkillBtnClick(GameObject go)
    {
        int SkillSlot = int.Parse(go.name);
        this.UseSkillAbility(SkillSlot);
    }

    public void UseSkillAbility(int abilityIndex)
    {
        Hashtable abiliIndexMap = (Hashtable)((object[])MainUnit.UnitTable[":abilities"])[abilityIndex - 1];
        Ability curAbiliy = MainUnit.Abilities[(string)abiliIndexMap[":name"]];
        CS_AbilityEvent abiliEvent = new CS_AbilityEvent();
        abiliEvent.abilityKey = curAbiliy.AbilityKey;
        abiliEvent.unitId = MainUnit.UnitId;
        Game.Net.SendPacket<CS_AbilityEvent>(abiliEvent, PBMessageID.CS_AbilityEvent);
    }

    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            this.UseSkillAbility(3);
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            this.UseSkillAbility(4);
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            this.UseSkillAbility(5);
        }

    }


}
